package com.practice.design.expressiontree;

public class ExpressionTree {
	private ExpressionNode 	root;
	
	public void build(String expression) {
		//TODO 
	}
	
	public int eval() {
		return root.eval();
	}
}

interface ExpressionNode {
	public int eval();
}

class Operator implements ExpressionNode {
	private int number;
	public Operator(int number) {
		this.number = number;
	}
	public int eval() {
		return number;
	}
}

abstract class Operand implements ExpressionNode {
	protected ExpressionNode		left;
	protected ExpressionNode		right;
	
	public void addLeft(ExpressionNode left) {
		this.left = left;
	}
	
	public void addRight(ExpressionNode left) {
		this.right = right;
	}
}

class Add extends Operand {
	public int eval() {
		return left.eval() + right.eval();
	}
}

class Subtract extends Operand {
	public int eval() {
		return left.eval() - right.eval();
	}
}

class Time extends Operand {
	public int eval() {
		return left.eval() * right.eval();
	}
}

class Divide extends Operand {
	public int eval() {
		return left.eval() / right.eval();
	}
}

class UnaryPositive extends Operand {
	public int eval() {
		return right.eval();
	}
}

class UnaryNegative extends Operand {
	public int eval() {
		return -right.eval();
	}
}

class Power extends Operand {
	public int eval() {
		return (int) Math.pow(left.eval(), right.eval());
	}
}

class PlusPlus extends Operand {
	public int eval() {
		return left.eval() + 1;
	}
}
